package com.ebelter.ty.ui.fragment.scale;


import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.ebelter.btcomlib.bases.fragment.BaseFragment;
import com.ebelter.btcomlib.common.Constants;
import com.ebelter.btcomlib.models.bluetooth.base.BlueManager;
import com.ebelter.btcomlib.models.eventbus.CommonEventBus;
import com.ebelter.btcomlib.utils.SpUtil;
import com.ebelter.btcomlib.utils.StringUtils;
import com.ebelter.btcomlib.utils.ToastUtil;
import com.ebelter.btcomlib.utils.UserSpUtil;
import com.ebelter.btcomlib.utils.log.LogUtils;
import com.ebelter.btcomlib.views.dialogs.TextTipDialog;
import com.ebelter.ty.R;
import com.ebelter.ty.ui.activity.DeviceManagerActivity;
import com.ebelter.ty.ui.activity.FindPswActivity;
import com.ebelter.ty.ui.activity.FragmentsActivity;
import com.ebelter.ty.ui.activity.LoginActivity;
import com.ebelter.ty.ui.activity.ScaleMainActivity;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;


/**
 * 血糖
 */

public class Scale_M_B5_Fragment extends BaseFragment implements View.OnClickListener {


    public static final String TAG = "Scale_M_B5_Fragment";
    private View set_name_rl;
    private View set_height_rl;
    private View set_birthday_rl;
    private View set_passcode_rl;
    private TextView set_exit_tv;
    private View set_devicemanager_rl;
    private View set_clearuserinfo_rl;
    private View set_about_rl;


   
    TextView page_set_name_tv;
    TextView page_set_stature_tv;
    TextView page_set_birthday_tv;


    @Override
    protected int setLayoutResouceId() {
        return R.layout.scale_m_buttom5_set;
    }

    @Override
    protected void initView() {
        super.initView();
        EventBus.getDefault().register(this);
        FV();
    }

    @Override
    protected void initData(Bundle arguments) {
        super.initData(arguments);
        disBirthday();
        disHeight();
        disName();
    }

    private void FV() {
        set_name_rl = mRootView.findViewById(R.id.set_name_rl);
        set_height_rl = mRootView.findViewById(R.id.set_height_rl);
        set_birthday_rl = mRootView.findViewById(R.id.set_birthday_rl);
        set_passcode_rl = mRootView.findViewById(R.id.set_passcode_rl);
        set_devicemanager_rl = mRootView.findViewById(R.id.set_devicemanager_rl);
        set_clearuserinfo_rl = mRootView.findViewById(R.id.set_clearuserinfo_rl);
        set_about_rl = mRootView.findViewById(R.id.set_about_rl);
        set_exit_tv = mRootView.findViewById(R.id.set_exit_tv);

        set_name_rl.setOnClickListener(this);
        set_height_rl.setOnClickListener(this);
        set_birthday_rl.setOnClickListener(this);
        set_passcode_rl.setOnClickListener(this);
        set_devicemanager_rl.setOnClickListener(this);
        set_clearuserinfo_rl.setOnClickListener(this);
        set_about_rl.setOnClickListener(this);
        set_exit_tv.setOnClickListener(this);

        page_set_name_tv = mRootView.findViewById(R.id.page_set_name_tv);
        page_set_stature_tv = mRootView.findViewById(R.id.page_set_stature_tv);
        page_set_birthday_tv = mRootView.findViewById(R.id.page_set_birthday_tv);
        page_set_name_tv.setOnClickListener(this);
        page_set_stature_tv.setOnClickListener(this);
        page_set_birthday_tv.setOnClickListener(this);

    }




    private void openActivity(int which) {
        Intent intent=new Intent(getActivity(),FragmentsActivity.class);
        intent.putExtra(FragmentsActivity.openWhichFragment,which);
        startActivity(intent);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.set_name_rl:
                openActivity(FragmentsActivity.SCALE_M_NAME);
                break;
            case R.id.set_height_rl:
                openActivity(FragmentsActivity.SCALE_M_HEIGHT);
                break;
            case R.id.set_birthday_rl:
                openActivity(FragmentsActivity.SCALE_M_BIRTHDAY);
                break;
            case R.id.set_passcode_rl:
                Intent intent=new Intent(getActivity(), FindPswActivity.class);
                getActivity().startActivity(intent);
                break;

            case R.id.set_exit_tv:
                exitLogin();
                break;
            case R.id.set_clearuserinfo_rl:
                clearUserInfo();
                break;
            case R.id.set_devicemanager_rl:
                Intent intent2=new Intent(getActivity(), DeviceManagerActivity.class);
                if (getActivity()!=null)getActivity().startActivity(intent2);
                break;
            case R.id.set_about_rl:
                openActivity(FragmentsActivity.SCALE_ABOUT);
                break;


        }
    }

    private void clearUserInfo() {

        LogUtils.i(TAG,"----set_clearuserinfo_rl_click");
        if (!BlueManager.getInstance().isBluetoothOpen()){
            ToastUtil.show(R.string.no_bluetooth_enabled);
            return;
        }
        if (!((ScaleMainActivity)getActivity()).scalebleIsConnect()){
            ToastUtil.show(R.string.do_not_attach_the_scale);
            return;
        }

        TextTipDialog tipDialog=new TextTipDialog(getActivity(), StringUtils.getResStr(R.string.del_all_user_tip));
        tipDialog.show();
        tipDialog.setListener(new TextTipDialog.ICancelOkBtListener() {
            @Override
            public void cancel(Dialog dialogView) {
                dialogView.cancel();
            }

            @Override
            public void ok(Dialog dialogView) {
                dialogView.cancel();
//                SpUtil.writeLong(Constants.IUser.CLEAR_ALLUSER_INFO_TIME,System.currentTimeMillis());
//                SpUtil.writeBoolean(Constants.IUser.CLEAR_ALLUSER_INFO_DIALOG_HASSHOW,false);
                ((ScaleMainActivity)getActivity()).requestDelAllUser();

            }
        });



    }

    private void disBirthday() {
        if (page_set_birthday_tv!=null){
            String birthday = UserSpUtil.readString(Constants.IUser.BIRTHDAY);
            page_set_birthday_tv.setText(birthday);
        }
    }

    private void disHeight() {

        if (page_set_stature_tv!=null){
            int h = UserSpUtil.readInt(Constants.IUser.HEIGHT);
            page_set_stature_tv.setText(h+" cm");
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    private void disName() {
        if (page_set_name_tv!=null){
            String name = UserSpUtil.readString(Constants.IUser.NICK_NAME);
            page_set_name_tv.setText(name);
        }
    }

    private void exitLogin() {
        LogUtils.i(TAG,"----exit_click");
        UserSpUtil.removeAll();
        Intent loginIntent =new Intent(getActivity(), LoginActivity.class);
        if (getActivity()!=null){
            getActivity().startActivity(loginIntent);
            getActivity().finish();}
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(CommonEventBus event) {
        if (!TextUtils.equals(event.whichReceive,TAG)){
            return;
        }
        LogUtils.i(TAG,"Scale_M_B5_Fragment--onEventMainThread接收到CommonEventBus信息--"+event.toString());
        if (event.cmd==0){
            disName();
        }else if (event.cmd==1){
            disHeight();
        }else if (event.cmd==2){
            disBirthday();
        }else if (event.cmd==3){//设备管理器清除了绑定的蓝牙地址

        }


    }
}
